package com.flower.service.impl;

import com.flower.mapper.RoleMapper;
import com.flower.model.dto.RoleDto;
import com.flower.model.dto.RolePageQueryDto;
import com.flower.model.vo.RoleVo;
import com.flower.result.PageResult;
import com.flower.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@SuppressWarnings("all")
/**
 *@ClassName RoleServiceImpl
 *@Description TODO
 *@Author @O_o  GW__
 *@Date 2025/6/12 16:46
 *@Version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public List<RoleVo> getAllRole() {
         List<RoleVo> roleVos =roleMapper.getAllRoles();
         return roleVos;
    }

    @Override
    public PageResult getRoleList(RolePageQueryDto rolePageQueryDto) {
        PageHelper.startPage(rolePageQueryDto.getPageNum(), rolePageQueryDto.getPageSize());
        Page<RoleVo> page = roleMapper.pageQuery(rolePageQueryDto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void updateRole(RoleDto roleDto) {
        if(roleDto.getId()!= null){
            log.info("修改角色开始");
            roleMapper.updateById(roleDto);
            log.info("修改完成");
        }else {
            log.info("添加角色开始");
            roleMapper.insertRole(roleDto);
            log.info("成功添加角色");
        }
    }

    @Override
    public void deleteRole(Integer id) {
        roleMapper.delRoleById(id);
    }

    @Override
    public void deleteRoleList(List<Integer> ids) {
        ids.forEach(id -> roleMapper.delRoleById(id));
    }

    @Override
    public List<Integer> getRoleMenu(Integer roleld) {
        List<Integer> roleMenuList=  roleMapper.getRoleMenuById(roleld);
        return roleMenuList;
    }

    @Override
    @Transactional
    public void updateRoleMenu(Integer roleId, List<Integer> menuIds) {
        roleMapper.deleteRoleMenuByRoleId(roleId);
        roleMapper.batchInsertRoleMenu(roleId,menuIds);
    }
}
